package com.fbs.wowo.finance.service.imp;


import com.fbs.wowo.finance.entity.Bill;
import com.fbs.wowo.finance.mapper.BillMapper;
import com.fbs.wowo.finance.service.BillService;
import com.fbs.wowo.finance.util.BillGenerator;
import com.fbs.wowo.finance.vo.BillQuery;
import com.fbs.wowo.finance.vo.NewBill;
import com.fbs.wowo.util.common.Generator;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

// TODO 退款方法
@Service("billService")
@Transactional
public class BillServiceImp implements BillService {

    @Override
    public void add(NewBill newBill) {
        // TODO 各种判空
        Bill record = new Bill();
        // 主键uuid
        record.setId(Generator.generateId());
        // 用户id
        record.setUserId(newBill.getUserId());
        // 交易时间
        record.setTradeTime(new Date());
        // 金额
        record.setTotalFee(newBill.getTotalFee());
        // 收入或支出 0收入 1支出
        record.setIsIncome(newBill.getIsIncome());
        // 对方的id
        record.setOtherSide(newBill.getOtherSide());
        // 操作前的余额
        record.setBalance(newBill.getBalance());
        // 退款id
        record.setRefund(newBill.getRefund());
        // 产品id
        record.setProductId(newBill.getProductId());
        // 产品类型
        record.setProductType(newBill.getProductType());
        // 产品分类
        record.setProductCategory(newBill.getProductCategory());
        // 交易状态
        record.setStatus(newBill.getStatus());
        // 支付类型
        record.setTradeType(newBill.getTradeType());
        // 交易场景（扫码支付 - 给XXX）
        record.setScene(newBill.getScene());
        // tip
        record.setTip(newBill.getTip());

        // 单号：
        // 支付渠道（web，手机），币种，支付方式（支付宝，微信），类型（转账，红包），时间戳
        // BillGenerator.AccessPhone
        // BillGenerator.CoinWOT
        // BillGenerator.PayNo
        // BillGenerator.TransTransfer
        String tradeId = BillGenerator.tradeId(
                newBill.getAccessType(),
                newBill.getTradeType(),
                newBill.getProductCategory(),
                newBill.getProductType());
        record.setTradeId(tradeId);

        billMapper.insertSelective(record);
    }

    /**
     * 按用户id查找交易记录
     */
    @Override
    public List<Bill> bills(BillQuery record) {
        // TODO 各种非空判断
        PageHelper.startPage(record.getPageNo(), record.getPageSize());
        List<Bill> selective = billMapper.findSelective(record);
        return selective;
    }

    @Autowired
    private BillMapper billMapper;

}
